/**
 * @Project : ProgrammingExercises
 * @Package : Leetcode
 * @File : Q2530.java
 * @Author : WangRuoyu
 * @Date 2023/1/25 18:09
 */

package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

// TODO
public class Q2530 {
    static int ceil(int x) {
        if (x % 3 != 0) {
            return x / 3 + 1;
        } else {
            return x / 3;
        }
    }

    public long maxKelements(int[] nums, int k) {
        Arrays.sort(nums);

        int lenth = nums.length;
        long res = 0;
        List<Integer> list = new ArrayList<>();

        for (int i = 0; i < lenth; i++) {
            list.add(nums[i]);
        }

        for (int i = 0; i < k; i++) {
            int tmp = list.get(lenth - 1);
            int index = 0;
            res += tmp;
            tmp = ceil(tmp);

            while (list.get(index) < tmp) {
                index++;
            }

            list.remove(lenth - 1);
            list.add(index, tmp);
            System.out.println(list);
        }

        return res;
    }

    public static void main(String[] args) {
        Q2530 sl = new Q2530();
        int[] nums = {1, 10, 3, 3, 3};
        int k = 3;
        System.out.println(sl.maxKelements(nums, k));
    }
}